package com.hzncc.flowable_diboot.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diboot.core.holder.ThreadLocalHolder;
import com.diboot.scheduler.annotation.CollectThisJob;
import com.hzncc.flowable_diboot.contract.entity.SalesContract;
import com.hzncc.flowable_diboot.contract.service.SalesContractService;
import com.hzncc.flowable_diboot.entity.invoicemanage.Vbrk;
import com.hzncc.flowable_diboot.service.SalesSignInService;
import com.hzncc.flowable_diboot.service.invoicemanage.InvoiceRegisterService;
import com.hzncc.flowable_diboot.service.invoicemanage.VbrkService;
import com.hzncc.flowable_diboot.vo.invoicemanage.SyncInvoiceRegisterVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.util.ThreadContext;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.ZoneId;
import java.util.List;

@DisallowConcurrentExecution
@CollectThisJob(
        name = "开票登记同步",
//        cron = "0 0 0 * * ?"
        cron = "0 0 1 * * ? *"
)
@Slf4j
public class InvoiceSyncJob extends QuartzJobBean implements ApplicationContextAware {

    @Resource
    private SecurityManager securityManager;

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        VbrkService vbrkService = applicationContext.getBean(VbrkService.class);
        SalesContractService salesContractService = applicationContext.getBean(SalesContractService.class);
        InvoiceRegisterService invoiceRegisterService = applicationContext.getBean(InvoiceRegisterService.class);
        ThreadContext.bind(securityManager);
        QueryWrapper<Vbrk> vbrkQuery = new QueryWrapper<>();
        vbrkQuery.eq("crmflaggw","1");
        ThreadLocalHolder.setIgnoreInterceptor();
        List<Vbrk> vbrkList = vbrkService.list(vbrkQuery);
        if (!CollectionUtils.isEmpty(vbrkList)){
            for (Vbrk vbrk : vbrkList) {
                // 合同项目信息
                LambdaQueryWrapper<SalesContract> salesContractQuery = new LambdaQueryWrapper<>();
                salesContractQuery.eq(SalesContract::getSapCode,vbrk.getVbelnH());
                // 设置忽略拦截器
                ThreadLocalHolder.setIgnoreInterceptor();
                SalesContract salesContract = salesContractService.getSingleEntity(salesContractQuery);
                if (ObjectUtils.isEmpty(salesContract)){
                    log.info("{}销售合同不存在",vbrk.getVbelnH());
                    continue;
                }
                SyncInvoiceRegisterVO syncInvoiceRegisterVO = new SyncInvoiceRegisterVO();
                syncInvoiceRegisterVO.setSalesOrganization(vbrk.getVkorg())
                        .setInvoiceCategory(vbrk.getFkart())
                        .setSapCode(vbrk.getVbelnH())
                        .setSapContractItem(vbrk.getPosnrH())
                        .setSapOrderCode(vbrk.getVbelnX())
                        .setSapDeliveryCode(vbrk.getVbelnJ())
                        .setSapCertCode(vbrk.getVbelnVf())
                        .setSapInvoiceItem(vbrk.getPosnrVf())
                        .setInvoiceCertTime(vbrk.getFkdat().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime())
                        .setInvoiceAmount(new BigDecimal(vbrk.getNetwr()))
                        .setPayer(vbrk.getKunrg())
                        .setCrmFlag(vbrk.getCrmflag())
                        .setDelFlag(vbrk.getDelflag());
                Boolean syncResult = invoiceRegisterService.syncInvoiceRegister(syncInvoiceRegisterVO);
                if (syncResult){
                    LambdaUpdateWrapper<Vbrk> vbrkUpdate = new LambdaUpdateWrapper<>();
                    vbrkUpdate.set(Vbrk::getCrmflaggw,"0").eq(Vbrk::getVkorg,vbrk.getVkorg()).eq(Vbrk::getFkart,vbrk.getFkart()).eq(Vbrk::getVbelnVf,vbrk.getVbelnVf()).eq(Vbrk::getPosnrVf,vbrk.getPosnrVf());
                    ThreadLocalHolder.setIgnoreInterceptor();
                    vbrkService.update(vbrkUpdate);
                }
            }
        }
    }
}
